package com.block.api.controller.admin;

import com.alibaba.fastjson.JSONObject;
import com.block.account.service.IAccountApplyRecordService;
import com.block.common.bean.CommonParam;
import com.block.common.exception.ApiException;
import com.block.common.security.util.ApiUtil;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.AppEncrypt;
import com.block.merchant.service.IMerchantPayOrderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("api/admin/account")
@Validated
public class AdminAccountController {

    @Autowired
    IAccountApplyRecordService accountApplyRecordService;
    @Autowired
    IMerchantPayOrderInfoService merchantPayOrderInfoService;

    /**
     * 入账
     */
    @RequestMapping(value = "/enterAccount", method = {RequestMethod.POST, RequestMethod.GET})
    public String enterAccount(@RequestAttribute(value = "tranData") JSONObject tranData,
                             @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                             @RequestAttribute(value = "uri") String uri,
                             @RequestAttribute(value = "commonParam") CommonParam commonParam,
                             @RequestAttribute(value = "startDate") Date startdate,
                             @RequestAttribute(value = "ip") String ip) throws ApiException {

        String orderNo = tranData.getString("orderNo");
        String operatorId = tranData.getString("operatorId");
        String operatorName = tranData.getString("operatorName");

        ResponseVo responseVo = merchantPayOrderInfoService.settlePayOrder(orderNo, operatorId, operatorName);

        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

    /**
     * 购买套餐
     */
    @RequestMapping(value = "/buyMerchantMeal", method = {RequestMethod.POST, RequestMethod.GET})
    public String buyMerchantMeal(@RequestAttribute(value = "tranData") JSONObject tranData,
                               @RequestAttribute(value = "appEncrypt") AppEncrypt appEncrypt,
                               @RequestAttribute(value = "uri") String uri,
                               @RequestAttribute(value = "commonParam") CommonParam commonParam,
                               @RequestAttribute(value = "startDate") Date startdate,
                               @RequestAttribute(value = "ip") String ip) throws ApiException {

        String orderNo = tranData.getString("infoId");
        String mealInfo = tranData.getString("mealInfo");
        String operatorId = tranData.getString("operatorId");
        String operatorName = tranData.getString("operatorName");

        ResponseVo responseVo = merchantPayOrderInfoService.settlePayOrder(orderNo, operatorId, operatorName);

        return ApiUtil.dealReturnJson(responseVo, appEncrypt,  startdate, uri);
    }

}
